package org.shuma.goods.service.impl;

import com.alipay.sofa.runtime.api.annotation.SofaReference;
import com.alipay.sofa.runtime.api.annotation.SofaReferenceBinding;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import org.shuma.goods.contant.GoodsConstant;
import org.shuma.goods.contant.UserConstant;
import org.shuma.goods.domain.entity.ProductBasicInfo;
import org.shuma.goods.domain.vo.ProductOnAndDownLineVo;
import org.shuma.goods.mapper.ProductBasicInfoMapper;
import org.shuma.goods.service.ProductUpAndDownLineService;
import org.shuma.goods.utils.HttpCodeEnum;
import org.shuma.goods.utils.Result;
import org.shuma.proxy.facade.Proxy;
import org.shuma.user.vo.UserVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * @author : liuming
 * @date : 2024/6/19 上午9:14
 */
@Service
public class ProductUpAndDownLineServiceImpl implements ProductUpAndDownLineService {
    @Autowired
    private ProductBasicInfoMapper productBasicInfoMapper;
    @SofaReference(interfaceType = Proxy.class, uniqueId = "${service.proxy.unique.id}",
            binding = @SofaReferenceBinding(bindingType = "bolt"))
    private Proxy proxy;

    @Override
    public Result upLine(ProductOnAndDownLineVo productOnAndDownLineVo, UserVo userVo) {
        Result result = new Result();
        if (userVo.getUserRole() == UserConstant.USER_STATUS) {
//            查询id列表中 所有已审核商品
            LambdaQueryWrapper<ProductBasicInfo> productBasicInfoLambdaQueryWrapper = new LambdaQueryWrapper<>();
            productBasicInfoLambdaQueryWrapper
//                    id列表中的id
                    .in(ProductBasicInfo::getProductId, productOnAndDownLineVo.getProduct_ids())
//                    商品状态为已审核
                    .eq(ProductBasicInfo::getProductCheckState, GoodsConstant.PASS_STATUS);
            List<ProductBasicInfo> productBasicInfos = productBasicInfoMapper.selectList(productBasicInfoLambdaQueryWrapper);
//            过滤掉不是该小二创建的商品+不是该小二代理的商品
            List<ProductBasicInfo> collect = productBasicInfos.stream().filter((productBasicInfo) -> {
                        boolean isProxyByUser = proxy.getProxy(productBasicInfo.getProductId())
                                .contains(userVo.getId());
                        boolean isCreateByUser = Objects.equals(productBasicInfo.getCreator(), userVo.getId());
                        return isCreateByUser || isProxyByUser;
                    }
            ).collect(Collectors.toList());
            // 获取collect 中所有的id 为列表
            List<Long> productIds = collect.stream().map(ProductBasicInfo::getProductId).collect(Collectors.toList());
            if (!collect.isEmpty()) {
                productBasicInfoMapper.update(null, new LambdaUpdateWrapper<ProductBasicInfo>()
                        .in(ProductBasicInfo::getProductId, productIds)
                        .set(ProductBasicInfo::getProductOwnerState, GoodsConstant.CONFIRM_ONLINE));
                result.ok();
            } else {
                result.error(HttpCodeEnum.NO_FIND, "商品不存在");
            }
        }
        return result;
    }

    @Override
    public Result downLine(ProductOnAndDownLineVo productOnAndDownLineVo, UserVo userVo) {
        Result result = new Result();
        if (userVo.getUserRole() == UserConstant.USER_STATUS) {
//            查询id列表中 所有已上线商品
            LambdaQueryWrapper<ProductBasicInfo> productBasicInfoLambdaQueryWrapper = new LambdaQueryWrapper<>();
            productBasicInfoLambdaQueryWrapper
//                    id列表中的id
//                    .in(ProductBasicInfo::getProductId, productOnAndDownLineVo.getProduct_ids())
//                    商品状态为已上线
                    .eq(ProductBasicInfo::getProductCheckState, GoodsConstant.GO_ONLINE);
            List<ProductBasicInfo> productBasicInfos = productBasicInfoMapper.selectList(productBasicInfoLambdaQueryWrapper);
//            过滤掉不是该小二创建的商品+不是该小二代理的商品
            List<ProductBasicInfo> collect = productBasicInfos.stream().filter((productBasicInfo) -> {
                        boolean isContains = proxy.getProxy(productBasicInfo.getProductId())
                                .contains(userVo.getId());
                        boolean isEquals = Objects.equals(productBasicInfo.getCreator(), userVo.getId());
                        return isEquals || isContains;
                    }
            ).collect(Collectors.toList());
            // 获取collect 中所有的id 为列表
            List<Long> productIds = collect.stream().map(ProductBasicInfo::getProductId).collect(Collectors.toList());

            if (!collect.isEmpty()) {
                productBasicInfoMapper.update(null, new LambdaUpdateWrapper<ProductBasicInfo>()
                        .in(ProductBasicInfo::getProductId, productIds)
                        .set(ProductBasicInfo::getProductOwnerState, GoodsConstant.BE_OFFLINE));
                result.ok();
            } else {
                result.error(HttpCodeEnum.NO_FIND, "商品不存在");
            }
        } else if (userVo.getUserRole() == UserConstant.ADMIN_STATUS) {
//            超级管理员
            LambdaUpdateWrapper<ProductBasicInfo> productBasicInfoLambdaQueryWrapper = new LambdaUpdateWrapper<>();
            productBasicInfoLambdaQueryWrapper
//                    id列表中的id
                    .in(ProductBasicInfo::getProductId, productOnAndDownLineVo.getProduct_ids())
//                    商品状态为已上线
                    .eq(ProductBasicInfo::getProductCheckState, GoodsConstant.GO_ONLINE)
                    .set(ProductBasicInfo::getProductOwnerState, GoodsConstant.BE_OFFLINE);
            int update = productBasicInfoMapper.update(null, productBasicInfoLambdaQueryWrapper);
            if (update > 0) {
                result.ok();
            } else {
                result.error(HttpCodeEnum.NO_FIND, "商品不存在");
            }
        }
        return result;
    }
}
